package com.zqxx.examine.portal.controller.action;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.model.exam.UserQuestionHistory;
import com.zqxx.examine.model.question.QuestionHistory;
import com.zqxx.examine.portal.security.UserInfo;
import com.zqxx.examine.portal.service.QuestionHistoryService;
import com.zqxx.examine.portal.service.QuestionService;

@Controller
public class PracticeAction
{
  @Autowired
  private QuestionService questionService;
  @Autowired
  private QuestionHistoryService questionHistoryService;
  
  @RequestMapping(value={"/student/practice-improve"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message submitPractice(@RequestBody QuestionHistory qh)
  {
    Message msg = new Message();
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    

    UserQuestionHistory history = new UserQuestionHistory();
    history.setQuestionId(qh.getQuestionId());
    history.setUserId(userInfo.getUserid());
    history.setQuestionTypeId(qh.getQuestionTypeId());
    boolean isRight = qh.getAnswer().equals(qh.getMyAnswer());
    history.setRight(isRight);
    try
    {
      this.questionHistoryService.addUserQuestionHist(new UserQuestionHistory[] { history });
    }
    catch (Exception e)
    {
      msg.setResult(e.getClass().getName());
      e.printStackTrace();
    }
    return msg;
  }
}

